//
//  YBNewsViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/11/29.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBNewsViewController.h"
#import "LPNewsTopViewController.h"
#import "LPNewsListCommonViewController.h"
#import "LPSearchViewController.h"
@interface YBNewsViewController ()
/**  标题控制器  */
@property (strong, nonatomic) GTPageController  *gtPageVc;
/**  栏目数据  */
@property (strong, nonatomic) NSArray  *dataSouce;
/**    */
@property (strong, nonatomic) UILabel  *serchLabel;
/**    */
@property (strong, nonatomic) UIImageView  *rightImge;


@end

@implementation YBNewsViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.dataSouce.count == 0) {
        [self getDataSource];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.hidden = YES;
    [self.navigView addSubview:self.serchLabel];
    [self.serchLabel addSubview:self.rightImge];
}

- (void)tapSearch{
    LPSearchViewController *vc = [LPSearchViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 数据
- (void)getDataSource{
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"UNIT_ID"] = ZJUSERINFORMODEL.CURRENT_USER_MODEL.UNITID;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getListNewsColumans] Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            self.dataSouce = [NSArray yy_modelArrayWithClass:[GTBasePageCommonModel class] json:object[@"NEW_COLUMNS"]];
            [self layoutUI];
        }else if ([object[@"STATE"] isEqualToString:@"60006"]){
            [YJProgressHUD showMessage:@"获取新闻栏目失败" inView:[UIApplication sharedApplication].keyWindow];
        }
    } Fail:^(id object) {
        
    }];
    
}


#pragma mark - 界面
- (void)layoutUI{
    if (self.dataSouce.count != 0) {
        self.gtPageVc = [self getDefaultController];
        self.gtPageVc.view.frame = CGRectMake(0, navHeight , self.view.width, [UIScreen zj_screenHeight] - navHeight );
        [self.view addSubview:self.gtPageVc.view];
        [self addChildViewController:self.gtPageVc];
    }
    
}


#pragma mark - 私有方法
- (GTPageController *)getDefaultController{
    
    LPNewsListCommonViewController *vc = [[LPNewsListCommonViewController alloc]init];
     LPNewsTopViewController *vc1 = [[LPNewsTopViewController alloc]init];
    NSMutableArray *titles = [NSMutableArray array];
    for (GTBasePageCommonModel *model  in self.dataSouce) {
        [titles addObject:model.COLUMN_NAME];
    }
    
    
    NSMutableArray *viewControllers = [NSMutableArray array];
    for (int i = 0; i < titles.count; i ++) {
        if (i == 0) {
            [viewControllers addObject:[vc1 class]];
        }else{
            [viewControllers addObject:[vc class]];
        }
    }
    
    
    __block NSMutableArray *titlesWidth = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat width = [NSString sizeOfString:obj font:16 * [UIScreen zj_textFontScale]  width:self.view.width].width + 25 * [UIScreen zj_screenWScale];
        [titlesWidth addObject:@(width)];
    }];
    
    GTPageController *pageVC = [[GTPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    pageVC.pageAnimatable = YES;
    pageVC.lpNewsColmModelArray = self.dataSouce;
    pageVC.itemsWidths = titlesWidth;
    pageVC.postNotification = YES;
    pageVC.menuBGColor = [UIColor whiteColor];
    pageVC.menuViewStyle = GTMenuViewStyleLine;
    pageVC.progressColor = [UIColor themeColor];
    pageVC.titleColorSelected = [UIColor themeColor];
    pageVC.titleColorNormal = [UIColor textBlackColor];
    pageVC.titleSizeNormal = 16 * [UIScreen zj_textFontScale];
    pageVC.titleSizeSelected = 17 * [UIScreen zj_textFontScale];
    pageVC.menuHeight = 50 * [UIScreen zj_screenHScale];
    return pageVC;
}

- (UILabel *)serchLabel{
    if (!_serchLabel) {
        _serchLabel = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 * [UIScreen zj_screenWScale], 8 + LP_StatusBarHeight, [UIScreen zj_screenWith] - 70 * [UIScreen zj_screenWScale], 28)];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor lightGrayColor];
            label.text = @"输入关键字快速查找内容";
            label.backgroundColor = [UIColor whiteColor];
            label.font = LYPFONT(14);
            label.layer.cornerRadius = 12;
            label.layer.masksToBounds = YES;
            label.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSearch)];
            [label addGestureRecognizer:tap];
            label;
        });
    }
    return _serchLabel;
}

- (UIImageView *)rightImge{
    if (_rightImge == nil) {
        _rightImge  = [[UIImageView alloc] initWithFrame:CGRectMake(self.serchLabel.width - 35, 0, 28, 28)];
        _rightImge.image = [UIImage imageNamed:@"learn_soso"];
        _rightImge.contentMode = UIViewContentModeCenter;
    }
    return _rightImge;
}

@end
